package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.system.domain.project.software.SoftwareCopyright;
import com.ruoyi.system.domain.project.software.SoftwareCopyrightTeacher;
import com.ruoyi.system.mapper.project.SoftwareCopyrightMapper;
import com.ruoyi.system.mapper.project.SoftwareCopyrightTeacherMapper;
import com.ruoyi.system.service.project.ISoftwareCopyrightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 软件著作权信息，存储智能药房系统V1.1等著作权登记详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@Service
public class SoftwareCopyrightServiceImpl implements ISoftwareCopyrightService
{
    @Autowired
    private SoftwareCopyrightMapper softwareCopyrightMapper;
    @Autowired
    private SoftwareCopyrightTeacherMapper softwareCopyrightTeacherMapper;

    /**
     * 查询软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     * 
     * @param id 软件著作权信息，存储智能药房系统V1.1等著作权登记详情主键
     * @return 软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     */
    @Override
    public SoftwareCopyright selectSoftwareCopyrightById(Long id)
    {
        SoftwareCopyright softwareCopyright = softwareCopyrightMapper.selectSoftwareCopyrightById(id);
        SoftwareCopyrightTeacher softwareCopyrightTeacher = new SoftwareCopyrightTeacher();
        softwareCopyrightTeacher.setResearchId(id);
        List<SoftwareCopyrightTeacher> list = softwareCopyrightTeacherMapper.selectSoftwareCopyrightTeacherList(softwareCopyrightTeacher);
        softwareCopyright.setTeachers(list);
        return softwareCopyright;
    }

    /**
     * 查询软件著作权信息，存储智能药房系统V1.1等著作权登记详情列表
     * 
     * @param softwareCopyright 软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     * @return 软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     */
    @Override
    public List<SoftwareCopyright> selectSoftwareCopyrightList(SoftwareCopyright softwareCopyright)
    {
        List<SoftwareCopyright> list = softwareCopyrightMapper.selectSoftwareCopyrightList(softwareCopyright);
        for(SoftwareCopyright info : list){
            SoftwareCopyrightTeacher softwareCopyrightTeacher = new SoftwareCopyrightTeacher();
            softwareCopyrightTeacher.setResearchId(info.getId());
            List<SoftwareCopyrightTeacher> list1 = softwareCopyrightTeacherMapper.selectSoftwareCopyrightTeacherList(softwareCopyrightTeacher);
            info.setTeachers(list1);
        }
        return list;
    }

    /**
     * 新增软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     * 
     * @param softwareCopyright 软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     * @return 结果
     */
    @Override
    public int insertSoftwareCopyright(SoftwareCopyright softwareCopyright)
    {
        if(softwareCopyright.getId()!=null){
            SoftwareCopyrightTeacher softwareCopyrightTeacher = new SoftwareCopyrightTeacher();
            softwareCopyrightTeacher.setResearchId(softwareCopyright.getId());
            softwareCopyrightTeacher.setUserId(String.valueOf(softwareCopyright.getUserId()));
            softwareCopyrightTeacher.setUserName(softwareCopyright.getUserName());
            softwareCopyrightTeacherMapper.insertSoftwareCopyrightTeacher(softwareCopyrightTeacher);
        }else {
            int result = softwareCopyrightMapper.insertSoftwareCopyright(softwareCopyright);
            if(result>0){
                SoftwareCopyrightTeacher softwareCopyrightTeacher = new SoftwareCopyrightTeacher();
                softwareCopyrightTeacher.setResearchId(softwareCopyright.getId());
                softwareCopyrightTeacher.setUserId(String.valueOf(softwareCopyright.getUserId()));
                softwareCopyrightTeacher.setUserName(softwareCopyright.getUserName());
                softwareCopyrightTeacherMapper.insertSoftwareCopyrightTeacher(softwareCopyrightTeacher);
            }
        }
        return 1;
    }

    /**
     * 修改软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     * 
     * @param softwareCopyright 软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     * @return 结果
     */
    @Override
    public int updateSoftwareCopyright(SoftwareCopyright softwareCopyright)
    {
        return softwareCopyrightMapper.updateSoftwareCopyright(softwareCopyright);
    }

    /**
     * 批量删除软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     * 
     * @param ids 需要删除的软件著作权信息，存储智能药房系统V1.1等著作权登记详情主键
     * @return 结果
     */
    @Override
    public int deleteSoftwareCopyrightByIds(Long[] ids)
    {
        for(Long id : ids){
            SoftwareCopyrightTeacher softwareCopyrightTeacher = new SoftwareCopyrightTeacher();
            softwareCopyrightTeacher.setResearchId(id);
            List<SoftwareCopyrightTeacher> list = softwareCopyrightTeacherMapper.selectSoftwareCopyrightTeacherList(softwareCopyrightTeacher);
            if(!list.isEmpty()){
                for (SoftwareCopyrightTeacher ra : list){
                    softwareCopyrightTeacherMapper.deleteSoftwareCopyrightTeacherById(ra.getId());
                }
            }
        }
        return softwareCopyrightMapper.deleteSoftwareCopyrightByIds(ids);
    }

    /**
     * 删除软件著作权信息，存储智能药房系统V1.1等著作权登记详情信息
     * 
     * @param id 软件著作权信息，存储智能药房系统V1.1等著作权登记详情主键
     * @return 结果
     */
    @Override
    public int deleteSoftwareCopyrightById(Long id)
    {
        return softwareCopyrightMapper.deleteSoftwareCopyrightById(id);
    }
    /**
     * 根据证书编号查询软件著作权信息
     * @param data
     * @return
     */
    @Override
    public SoftwareCopyright getCopyrightInfoByCertificateNumber(String data) {
        return softwareCopyrightMapper.getCopyrightInfoByCertificateNumber(data);
    }
}
